<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="../../static/layuiadmin/layui/css/layui.css" tppabs="https://www.layui.site/layui/dist/css/layui.css" media="all">
    <script src="../../static/layuiadmin/layui/layui.js" charset="utf-8"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    <link rel="stylesheet" href="addDept.css">
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item" id="alter_form">
        <br><br>
        <div class="layui-inline" id="dept_name">
            <label class="layui-form-label">部门名称<i style="color: red">*</i></label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <br><br>
        <div class="layui-inline">
            <label class="layui-form-label">上级部门<i style="color: red">*</i></label>
            <div class="layui-input-inline">
                <input type="text" name="deptId" id="dept_id" hidden>
                <input type="text" name="parentId" id="parent_id" hidden>
                <input type="text" name="parentName" id="parentName" lay-verify="required" placeholder="一级部门" autocomplete="off" class="layui-input" onclick="load_deptTree()">
            </div>
        </div><br><br>

        <input type="text" style="display: none" id="create_by" name="createBy" /><br>


        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo2">立即修改</button>
        </div>
    </div>
    <br>
</form>

<script src="../../static/layuiadmin/layui/layui.js" charset="utf-8"></script>

<script type="application/javascript">
    layui.use(['form'], function () {

        var $ = layui.jquery
            ,form = layui.form
            ,disable = false

        //监听提交(修改部门)
        form.on('submit(demo2)', function (data) {
            event.preventDefault();
            if (disable) return
            disable = true
            $.ajax({
                type: "POST",
                url: "/ams-admin/dept/alter",
                data: JSON.stringify(data.field),
                contentType: "application/json",
                success: function (r) {
                    disable = false
                    if(r.code === 0){
                        layer.alert("修改成功！",function (){
                            window.parent.location.reload()
                            parent.layer.close(index)
                        })
                    }else {
                        layer.alert(r.msg);
                    }
                }
            });
        });
        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });
    });

    function load_deptTree() {
        layer.open({
            type: 2
            , title: "选择上级部门" //不显示标题栏
            , closeBtn: 1
            , area: ['400px', '300px']
            , shade: 0.8
            , btn: ['确定', '取消']
            , btnAlign: 'c'
            , offset: 'rb'
            , scrollbar: false
            , content: 'dept_tree.html'
        });
    }
</script>

</body>
</html>